package com.dg.cuckoo.admin.service.impl;

import com.dg.cuckoo.admin.dto.basicInformation.permissionSettings.UserToProDto;
import com.dg.cuckoo.admin.dto.basicInformation.projectInformation.BasicProjectDto;
import com.dg.cuckoo.admin.repository.BasicProjectRepository;
import com.dg.cuckoo.admin.repository.UserToProRepository;
import com.dg.cuckoo.admin.service.OrganizationService;
import com.dg.cuckoo.admin.service.ProjectInfoServer;
import com.dg.cuckoo.admin.service.UsertoProjectService;
import com.dg.cuckoo.core.ServiceException;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserProjectServiceimpl implements UsertoProjectService {
    @Autowired
    private OrganizationService organizationService;
    @Autowired
    private BasicProjectRepository basicProjectRepository;
    @Autowired
    private ProjectInfoServer projectInfoServer;
    @Autowired
    private UserToProRepository repository;

    @Override
    public String view() {
        String have = JSONObject.fromObject(organizationService.view()).getString("have");
        String no = JSONObject.fromObject(organizationService.view()).getString("no");

        Integer page=1;
        Integer pageSize=50;
        page = (page - 1) * pageSize;
        List<BasicProjectDto> dtos = basicProjectRepository.page(page, pageSize);
        List<String> name=new ArrayList<>();
        for(BasicProjectDto b:dtos){
            name.add(b.getMainName());
        }
        JSONObject json = new JSONObject();
        json.put("have",have);//用户名称所有的树
        json.put("no",no);//用户名称所在单位名称为空的
        json.put("name",name);//项目名称的名称list分页
        return json.toString();
    }

    @Override
    public String openInfoByname(String name) {
        if(name==null){
            throw new ServiceException("请刷新");
        }
        BasicProjectDto dto = basicProjectRepository.findByMainName(name);
        return projectInfoServer.openInfo(dto.getId());
    }

    @Override
    public void save(String username, List<String> list) {
        if(username==null){
            throw new ServiceException("请选择用户名称");
        }
        if(list.size()>0){
            List<UserToProDto> dtos = repository.findUserToProDtosByTypeName(username);
            repository.deleteAll(dtos);
            for(String s:list){
                UserToProDto dto = new UserToProDto();
                dto.setProName(username);
                dto.setTypeName(s);
                repository.save(dto);
            }
        }else {
            List<UserToProDto> dtos = repository.findUserToProDtosByTypeName(username);
            repository.deleteAll(dtos);
        }

    }

    @Override
    public List<String> search(String search) {
        if(search==null){
            throw new ServiceException("请输入要搜索条件");
        }
        List<BasicProjectDto> dtos = basicProjectRepository.findByMainNameContaining(search);
        List<String> list=new ArrayList<>();
        for(BasicProjectDto b:dtos){
            list.add(b.getMainName());
        }
        return list;
    }

    @Override
    public List<String> viewpro(String username) {
        if(username==null){
            username="";
        }
        List<UserToProDto> dtos = repository.findUserToProDtosByTypeName(username);
        List<String> list=new ArrayList<>();
        for(UserToProDto u:dtos){
            list.add(u.getProName());
        }
        return list;
    }
}
